"""
@File    :   models.py
@Time    :   2024/02/18 10:35:41
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   OAuth2.0 相关数据模型
"""

from datetime import datetime

from sqlalchemy import (
    JSON,
    BigInteger,
    Boolean,
    Column,
    DateTime,
    Integer,
    String,
    UniqueConstraint,
)

from src.config import get_app_settings
from src.models.models import OrmBase

settings = get_app_settings()


class OAuth2Client(OrmBase):
    """OAuth2客户端数据模型"""

    __tablename__ = "system_oauth2_client"
    __table_args__ = (
        UniqueConstraint("client_id", "tenant_id"),
        UniqueConstraint("name", "tenant_id"),
    )

    id = Column(BigInteger(), primary_key=True, autoincrement=True, comment="主键")
    status = Column(
        Boolean(), default=True, nullable=False, comment="状态"
    )  # (True正常 False停用)
    show_order = Column(
        Integer(), nullable=False, index=True, default=1, comment="显示顺序"
    )
    remark = Column(String(300), default="", nullable=False, comment="备注")
    create_datetime = Column(
        DateTime(), nullable=False, default=datetime.now, index=True, comment="创建时间"
    )
    update_datetime = Column(
        DateTime(),
        nullable=False,
        default=datetime.now,
        onupdate=datetime.now,
        index=True,
        comment="修改时间",
    )
    expiration_datetime = Column(
        DateTime(), nullable=False, index=True, comment="过期时间"
    )
    # 创建者用户名(级联删除)
    creator_username = Column(
        String(20), default="", nullable=False, comment="创建者用户名"
    )
    # 修改者用户名(级联删除)
    updater_username = Column(
        String(20), default="", nullable=False, comment="修改者用户名"
    )
    # 租户ID(级联删除)
    tenant_id = Column(BigInteger(), nullable=False, index=True, comment="租户ID")
    client_id = Column(String(255), nullable=False, comment="客户端编号")
    client_secret = Column(String(255), nullable=False, comment="客户端密钥")
    name = Column(String(100), nullable=False, comment="应用名")
    logo = Column(
        String(300),
        default=settings.default_oauth2_client_logo,
        nullable=False,
        comment="应用图标",
    )
    access_token_expiration_seconds = Column(
        Integer(),
        default=settings.oauth2_access_token_default_expiration_seconds,
        nullable=False,
        comment="访问令牌过期秒数",
    )  # 单位: 秒
    refresh_token_expiration_seconds = Column(
        Integer(),
        default=settings.oauth2_refresh_token_default_expiration_seconds,
        nullable=False,
        comment="刷新令牌过期秒数",
    )  # 单位: 秒
    authorization_code_expiration_seconds = Column(
        Integer(),
        default=settings.oauth2_authorization_code_default_expiration_seconds,
        nullable=False,
        comment="授权码过期秒数",
    )  # 单位: 秒
    redirect_uris = Column(
        JSON(), nullable=False, default=[], comment="可重定向的URI地址"
    )
    authorized_grant_types = Column(
        JSON(), nullable=False, default=[], comment="oauth2授权类型"
    )
    scopes = Column(JSON(), nullable=False, default=[], comment="oauth2授权范围")
    auto_approve_scopes = Column(
        JSON(), nullable=False, default=[], comment="自动赋予的oauth2授权范围"
    )
    additional_information = Column(
        String(4096), default="", nullable=False, comment="附加信息"
    )
    is_init = Column(
        Boolean(), default=False, nullable=False, comment="是否初始创建的Oauth2客户端"
    )
